package stream.demo1;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ZH
 */
public class StreamCollect {
    public static void main(String[] args) {
        List<Person> persons =
                Arrays.asList(
                        new Person("Max", 18),
                        new Person("Peter", 23),
                        new Person("Pamela", 23),
                        new Person("David", 12));
        //转换为集合
        List<Person> list = persons.stream()
                .filter(t -> t.name.startsWith("P"))
                .collect(Collectors.toList());
        //System.out.println(list);

        // * 分组
        Map<Integer, List<Person>> map = persons.stream()
                .collect(Collectors.groupingBy(t -> t.age));
        map.forEach((age,people) -> System.out.format("%s : %s\n",age,people));
        //求和
        //Double total = persons.stream().collect(Collectors.averagingInt(t -> t.age));
        //System.out.println(total);
        IntSummaryStatistics summary = persons.stream().collect(Collectors.summarizingInt(t -> t.age));
        System.out.println(summary);
        Arrays.asList(3, 1, 2, 1).stream().distinct().sorted().forEach(t-> System.out.println(t));
        List<Person> persons2 =
                Arrays.asList(
                        new Person("Max", 18),
                        new Person("Peter", 23),
                        new Person("Pamela", 23),
                        new Person("David", 12),
                        new Person("Max", 12));


    }
}
